<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/importer/trace_event_importer.html">
<link rel="import" href="/tracing/metrics/cpu_process_metric.html">
<link rel="import" href="/tracing/value/histogram_set.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  function makeModel(events) {
    return tr.c.TestUtils.newModelWithEvents([events]);
  }

  test('cpuProcessMetric_noData', function() {
    const histograms = new tr.v.HistogramSet();
    const events = [
      {name: 'a', args: {}, pid: 52, ts: 524, cat: 'foo', tid: 53, ph: 'B'},
      {name: 'a', args: {}, pid: 52, ts: 560, cat: 'foo', tid: 53, ph: 'B'}
    ];
    tr.metrics.sh.cpuProcessMetric(histograms, makeModel(events));
    assert.lengthOf(histograms, 0);
  });

  test('cpuProcessMetric_singleSnapshots', function() {
    const histograms = new tr.v.HistogramSet();
    const events = [
      {
        'name': 'CPUSnapshots',
        'args': {
          'snapshot': {
            'processes': [
              {'path': '/usr/sbin/crudd', 'pCpu': '99.0', 'pid': '13495',
                'pMem': '0.0', 'name': 'crudd'},
              {'path': '/opt/chrome/chrome', 'pCpu': '0.8',
                'pid': '29660', 'pMem': '0.9', 'name': 'chrome'},
              {'path': '/opt/chrome/chrome', 'pCpu': '0.3',
                'pid': '29661', 'pMem': '0.9', 'name': 'chrome'}
            ]
          }
        },
        'pid': 52, 'ts': '2226221225693.658', 'tid': 53, 'ph': 'O',
        'local': true, 'id': '0x1000'
      }
    ];
    tr.metrics.sh.cpuProcessMetric(histograms, makeModel(events));

    assert.isDefined(histograms.getHistogramNamed('cpu:percent:chrome'));
    assert.isDefined(histograms.getHistogramNamed('cpu:percent:crudd'));
    const chromeValue = histograms.getHistogramNamed('cpu:percent:chrome');
    const chromeStatistics = chromeValue.running;
    assert.strictEqual(chromeStatistics.count, 1);
    assert.closeTo(chromeStatistics.mean, 0.011, 1e-5);
    assert.closeTo(chromeStatistics.max, 0.011, 1e-5);
    assert.instanceOf(chromeValue.diagnostics.get('paths'), tr.v.d.GenericSet);
    const paths = tr.b.getOnlyElement(chromeValue.diagnostics.get('paths'));
    assert.strictEqual(paths, '/opt/chrome/chrome');
  });

  test('cpuProcessMetric_multipleSnapshots', function() {
    const histograms = new tr.v.HistogramSet();
    const events = [
      {
        'name': 'CPUSnapshots',
        'args': {
          'snapshot': {
            'processes': [
              {'path': '/usr/sbin/crudd', 'pCpu': '99.0', 'pid': '13495',
                'pMem': '0.0', 'name': 'crudd'},
              {'path': '/opt/chrome/chrome', 'pCpu': '0.8',
                'pid': '29660', 'pMem': '0.9', 'name': 'chrome'}
            ]
          }
        },
        'pid': 52, 'ts': '2226221225693.658', 'tid': 53,
        'ph': 'O', 'local': true, 'id': '0x1000'
      },
      {
        'name': 'CPUSnapshots',
        'args': {
          'snapshot': {
            'processes': [
              {'path': '/usr/sbin/crudd', 'pCpu': '1.3', 'pid': '13495',
                'pMem': '0.0', 'name': 'crudd'},
              {'path': '/opt/chrome/chrome', 'pCpu': '0.6',
                'pid': '29660', 'pMem': '0.9', 'name': 'chrome'},
              {'path': '/opt/chromium/chrome', 'pCpu': '0.1',
                'pid': '29660', 'pMem': '0.9', 'name': 'chrome'},
              {'path': '/usr/sbin/mnp_logger', 'pCpu': '0.2', 'pid': '6543',
                'pMem': '0.1', 'name': 'mnp_logger'}
            ]
          }
        },
        'pid': 52, 'ts': '2226222262064.4473', 'tid': 53,
        'ph': 'O', 'local': true, 'id': '0x1000'
      }
    ];
    tr.metrics.sh.cpuProcessMetric(histograms, makeModel(events));
    assert.isDefined(histograms.getHistogramNamed('cpu:percent:chrome'));
    assert.isDefined(histograms.getHistogramNamed('cpu:percent:crudd'));
    assert.isDefined(histograms.getHistogramNamed('cpu:percent:mnp_logger'));
    const chromeValue = histograms.getHistogramNamed('cpu:percent:chrome');
    const chromeStatistics = chromeValue.running;
    assert.strictEqual(chromeStatistics.count, 2);
    assert.closeTo(chromeStatistics.mean, 0.0075, 1e-5);
    assert.strictEqual(chromeStatistics.max, 0.008);
    assert.instanceOf(chromeValue.diagnostics.get('paths'), tr.v.d.GenericSet);
    const paths = Array.from(chromeValue.diagnostics.get('paths'));
    assert.lengthOf(paths, 2);
    assert.strictEqual(paths[0], '/opt/chrome/chrome');
  });
});
</script>
